package com.novax.ex.activity.provider.controller.inner;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.infrastructure.entity.TaskLanguage;
import com.novax.ex.activity.infrastructure.entity.TaskReward;
import com.novax.ex.activity.infrastructure.entity.TaskTitle;
import com.novax.ex.activity.infrastructure.entity.TaskTitleLanguage;
import com.novax.ex.activity.open.api.inner.TaskRewardApi;
import com.novax.ex.activity.open.api.inner.TaskTitleApi;
import com.novax.ex.activity.open.model.request.task.TaskLanguageRequest;
import com.novax.ex.activity.open.model.request.task.TaskRewardRequest;
import com.novax.ex.activity.open.model.response.task.TaskLanguageResponse;
import com.novax.ex.activity.open.model.response.task.TaskRewardInnerResponse;
import com.novax.ex.activity.open.model.response.task.TaskTitleResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.TaskLanguageService;
import com.novax.ex.activity.provider.service.TaskRewardService;
import com.novax.ex.activity.provider.service.TaskTitleService;
import com.novax.ex.common.base.BaseEntity;
import com.novax.ex.common.constant.ActivityConstants;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.core.redis.RedisUtil;
import com.novax.ex.common.enums.TaskLanguageType;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description 后台-标题简介配置
 * @Author ChenXi
 * @Date 2023/06/21/10:34
 */

@RestController
public class TaskTitleController extends BaseActivityController implements TaskTitleApi {

    @Resource
    private TaskTitleService taskTitleService;

    @Resource
    private TaskLanguageService taskLanguageService;

    @Override
    public ReturnResult<List<TaskTitleResponse>> queryAll() {
        return ReturnResult.success(CopyUtils.copyList(taskTitleService.queryAll(), TaskTitleResponse.class));
    }

    @Override
    public ReturnResult<List<TaskLanguageResponse>> queryAllByTitle(Long id) {
        return ReturnResult.success(CopyUtils.copyList(taskLanguageService.findByParentId(id, TaskLanguageType.title.getType()), TaskLanguageResponse.class));
    }

    @Override
    public ReturnResult<?> modify(String language, Long id, TaskLanguageRequest taskLanguageRequest) {
        // 语言不可更改
        taskLanguageRequest.setLanguage(null);
        TaskLanguage taskLanguage = CopyUtils.copyObject(taskLanguageRequest, TaskLanguage.class);
        taskLanguage.setId(id);
        boolean flag = taskLanguageService.modifySelective(taskLanguage);
        String repeatKey = ActivityConstants.redisType.TASK_TITLE_LANGUAGE + language;
        RedisUtil.delete(repeatKey);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
                : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> add(String language, Long id, TaskLanguageRequest taskLanguageRequest, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }

        Map<String, Object> query = new HashMap<>(2);
        query.put("language", taskLanguageRequest.getLanguage());
        query.put("parent_id", id);
        query.put("type", TaskLanguageType.title.getType());
        List<TaskLanguage> list = taskLanguageService.findList(query);

        // 新增时判断语言是否已配置
        if (!list.isEmpty()) {
            return ReturnResult.fail(getInternational(language, InternationalCode.LANGUAGE_EXIST));
        }

        TaskLanguage taskLanguage = CopyUtils.copyObject(taskLanguageRequest, TaskLanguage.class);
        taskLanguage.setType(TaskLanguageType.title.getType());
        taskLanguage.setParentId(id);
        boolean flag = taskLanguageService.addSelective(taskLanguage);
        ;
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
                : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

}
